﻿using System.Collections.Generic;
using RestSharp;
using Ymatou.API.Common;
using Ymatou.Evaluate.Buyer.Model.ApiModel.Response.Operator;
using Ymatou.Evaluate.Buyer.Model.Dto;
using Ymatou.ServiceCenter;

namespace Ymatou.Evaluate.Buyer.Model.ApiModel.Request.Operator
{
    /// <summary>
    /// 提交评价
    /// </summary>
    [Resource("bevaluateo.iapi.ymatou.com", "api/SubmitEvaluate", Method.POST, ParameterBuildType.Json)]
    public class SubmitEvaluateRequest : BaseRequestModel<SubmitEvaluateResponse>
    {
        public override string ApiName { get { return "api/SubmitEvaluate"; } }
        public override bool HttpGet { get { return false; } }

        /// <summary>
        /// 订单Id
        /// </summary>
        public int OrderId { get; set; }

        /// <summary>
        /// 买手Id
        /// </summary>
        public int SellerId { get; set; }

        /// <summary>
        /// 买家Id
        /// </summary>
        public int BuyerId { get; set; }

        /// <summary>
        /// 物流评分
        /// </summary>
        public decimal LogisticsScore { get; set; }

        /// <summary>
        /// 服务评分
        /// </summary>
        public decimal ServiceScore { get; set; }

        /// <summary>
        /// 商品评价列表
        /// </summary>
        public List<ProductEvaluate> ProductsEvaluate { get; set; }

        /// <summary>
        /// 买家提交评价时校验逻辑
        /// </summary>
        public override void Validate()
        {
            const string errCode = "-1";
            if (OrderId <= 0)
            {
                throw new BussniessException(errCode, "订单Id非法");
            }
            if (BuyerId <= 0)
            {
                throw new BussniessException(errCode, "买家Id非法");
            }
            if (SellerId <= 0)
            {
                throw new BussniessException(errCode, "买手Id非法");
            }
            if (LogisticsScore < 1 || LogisticsScore > 5)
            {
                throw new BussniessException(errCode, "物流评分非法[1-5]");
            }
            if (ServiceScore < 1 || ServiceScore > 5)
            {
                throw new BussniessException(errCode, "服务评分非法[1-5]");
            }
            if (ProductsEvaluate == null || ProductsEvaluate.Count <= 0)
            {
                throw new BussniessException(errCode, "评价商品不能为空");
            }
            ProductsEvaluate.ForEach(pro =>
            {
                if (string.IsNullOrEmpty(pro.CatalogId))
                {
                    throw new BussniessException(errCode, "规格Id存在非法");
                }
                if (!string.IsNullOrEmpty(pro.Comments) && pro.Comments.Length > 500)
                {
                    throw new BussniessException(errCode, "商品评价内容不能超过500字");
                }
                if (string.IsNullOrEmpty(pro.ProductId))
                {
                    throw new BussniessException(errCode, "商品Id存在非法");
                }
                if (pro.Score < 1 || pro.Score > 5)
                {
                    throw new BussniessException(errCode, "商品评分存在非法[1-5]");
                }
                if (pro.Imgs != null && pro.Imgs.Count > 6)
                {
                    throw new BussniessException(errCode, "商品评价图片不能超过6张");
                }
            });
            base.Validate();
        }

        /// <summary>
        /// 洗数据时校验逻辑
        /// </summary>
        public void ValidateAuto()
        {
            const string errCode = "-1";
            if (OrderId <= 0)
            {
                throw new BussniessException(errCode, "订单Id非法");
            }
            if (BuyerId <= 0)
            {
                throw new BussniessException(errCode, "买家Id非法");
            }
            if (SellerId <= 0)
            {
                throw new BussniessException(errCode, "买手Id非法");
            }
            if (LogisticsScore < 0 || LogisticsScore > 5)
            {
                throw new BussniessException(errCode, "物流评分非法[0-5]");
            }
            if (ServiceScore < 0 || ServiceScore > 5)
            {
                throw new BussniessException(errCode, "服务评分非法[0-5]");
            }
            if (ProductsEvaluate == null || ProductsEvaluate.Count <= 0)
            {
                throw new BussniessException(errCode, "评价商品不能为空");
            }
            ProductsEvaluate.ForEach(pro =>
            {
                if (string.IsNullOrEmpty(pro.CatalogId))
                {
                    throw new BussniessException(errCode, "规格Id存在非法");
                }

                if (string.IsNullOrEmpty(pro.ProductId))
                {
                    throw new BussniessException(errCode, "商品Id存在非法");
                }
                if (pro.Score < 0 || pro.Score > 5)
                {
                    throw new BussniessException(errCode, "商品评分存在非法[0-5]");
                }
            });
            base.Validate();
        }
    }
}
